package cardbs.servlets;

import static cardbs.dao.DAOFactory.*;
import static cardbs.dao.PartDAO.*;
import static cardbs.domain.Part.*;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cardbs.dao.DAOFactory;
import cardbs.dao.DepartmentDAO;
import cardbs.dao.EmployeeDAO;
import cardbs.dao.JPAUtil;
import cardbs.dao.LocatedAtDAO;
import cardbs.dao.PartDAO;
import cardbs.dao.WorkDAO;
import cardbs.domain.Department;
import cardbs.domain.Deptype;
import cardbs.domain.Employee;
import cardbs.domain.Located;
import cardbs.domain.Part;
import cardbs.domain.ServicePoint;
import cardbs.domain.UserType;
import cardbs.domain.Work;

public class UpdatePartInfoServlet extends HttpServlet {
	
	public UpdatePartInfoServlet() {
		// TODO Auto-generated constructor stub
	}
	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param request
	 * @param response
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		// begin-user-code
		// TODO Auto-generated method stub
		//parametroi apo to POST
		
		HttpSession session = request.getSession(true);
		Employee employee = (Employee)session.getAttribute("Administrator");
		String partName;
		String company;
		String manufacturyYear;
		Double price;
		int newStockInKA;
		int partCode;
		int i=0;
		int sum;
		String select;
		sum = Integer.parseInt(request.getParameter("sum"));
		for(i=0; i<=sum; i++){
			select = request.getParameter("select"+i);
			if(select != null){
				try {
					//parametroi apo to post
				partName = request.getParameter("name"+i);
				company = request.getParameter("company"+i);
				manufacturyYear = request.getParameter("year"+i);
				price = Double.parseDouble(request.getParameter("price"+i));
				partCode = Integer.parseInt(request.getParameter("part"+i));
				newStockInKA = Integer.parseInt(request.getParameter("stock"+i));
				DAOFactory daoFactory = DAOFactory.getInstance();
				PartDAO partDAO = daoFactory.getPartDAO();
				EntityManager em = JPAUtil.getCurrentEntityManager();
				EntityTransaction tx = em.getTransaction();
				tx.begin();
				Part part = partDAO.findPartByCodeUpdate(partCode);
                part.setCompany(company);
                part.setManufacturyYear(manufacturyYear);
                part.setPartName(partName);
                part.setPrice(price);
                tx.commit();
                LocatedAtDAO locatedAtDAO = daoFactory.getLocatedDAO();
                Located located = locatedAtDAO.find(part.getPartCode(),employee.getServicePoint().getServicePointCode());
                if(located!=null){
                	tx.begin();
                	located.setStock(newStockInKA);
                	tx.commit();
        		}else{
        			if(newStockInKA>0){
                	
                	Located newLocated = new Located();
                	newLocated.setStock(newStockInKA);
                	newLocated.setPart(part);
                	newLocated.setServicePoint(employee.getServicePoint());
                	locatedAtDAO.save(newLocated);
        			}
                }
                
				//Boolean part2 = partDAO.updatePartByCode(partCode,partName,company,manufacturyYear,price);
				
				HttpSession sess = request.getSession(true);
				sess.setAttribute("PartUpdated", "true");
				response.sendRedirect("/car-dbs-implementation/uipages/firstPagePages/AdministratorViewLoginPage.jsp");
				
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					} 
				} 
			}
		}
			
	}
